<?php

/**
 * uniBoard Strings Functions
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_strings{
	
	/**
	 * Validates e-mail address
	 *
	 * @param string $mail_address
	 * @return bool
	 */
		
	static function isMail( $mail_address){
				
		// Forbidden characters?
		if ( strpos( $mail_address, '(') !== false || strpos( $mail_address, ')') !== false)
		{
			return false;
		}
		
		if ( strpos( $mail_address, '[') !== false || strpos( $mail_address, ']') !== false)
		{
			return false;
		}
		
		if ( strpos( $mail_address, '\\') !== false)
		{
			return false;
		}
		
		if ( strpos( $mail_address, '"') !== false || strpos( $mail_address, "'") !== false || strpos( $mail_address, ';') !== false || strpos( $mail_address, '#') !== false || strpos( $mail_address, '%') !== false || strpos( $mail_address, '&') !== false || strpos( $mail_address, '*') !== false)
		{
			return false;
		}
				
		if ( strpos( $mail_address, '>') !== false || strpos( $mail_address, '>') !== false)
		{
			return false;
		}
		
		// Got "at" (or as europeans say, monkey?)?
		if ( strpos( $mail_address, '@') !== false)
		{
			// @ position?
			$at_pos = strpos( $mail_address, '@');
			
			// @ not at beginning of string?
			if ( $at_pos > 0)
			{
				// Dot that separates domain from adress
				$dot_pos = strrpos( $mail_address, '.', $at_pos);
				
				// Dot position correct?
				if ( strlen( $mail_address) - $dot_pos >= 2 && strlen( $mail_address) - $dot_pos <= 4)
				{
					//domain is ok
					return true;
				}
				else
				{
					//domain is wrong
					return false;
				}
			}
			else
			{
				//@ at beginning = url incorrect
				return false;
			}
		}
		else
		{
			//no @ = no email
			return false;
		}
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Converts bytes into kilobytes, megabytes or gigabytes
	 *
	 * @param integer $bytes
	 * @param string $force
	 * @return string
	 */
	
	static function countSize( $bytes, $force = ''){
				
		if ( $bytes >= 1073741824 || $force == 'gb')
		{
			// GigaBytes
			return round( $bytes / 1073741824, 2) . ' GB';
		}
		else if ( $bytes >= 1048576 || $force == 'mb')
		{
			// MegaBytes
			return round( $bytes / 1048576, 2) . ' MB';
		}
		else if ( $bytes >= 1024 || $force == 'kb')
		{
			// KiloBytes
			return round( $bytes / 1024, 3) . ' KB';
		}
		else
		{
			// Bytes
			return $bytes . ' B';
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * This Function removes all new-line characters
	 *
	 * @param string $string
	 * @return string
	 */
	
	static function stripLines( $string){
		
		return str_replace( "\r", '', str_replace( "\n", '', $string));
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * This Function removes all brackets
	 *
	 * @param string $string
	 * @return string
	 */
	
	static function stripBrackets( $string){
		
		return str_replace( '(', '', str_replace( ')', '', $string));
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * This Function replaces keys in mail template
	 *
	 * @param string $raw_text
	 * @param array $strings
	 * @return string
	 */
	
	static function parseMailText( $raw_text, $strings){
		
		// Return parsed string
		return str_replace( array_keys( $strings), $strings, $raw_text);
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Encodes string for insert into sprintf
	 *
	 * @param string $string
	 * @return string
	 */
	
	static function sprintfEnc( $string){
		
		return str_replace( '%', '%%', urlencode( $string));
	}
	
}